package com.czl.module_storehouse.adapter;

import android.content.Intent;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.remand.damage.DamageRegisProductActivity;
import com.czl.base.data.bean.tengyun.RecordRemandBean;
import com.czl.module_storehouse.event.DamageRegisSearchEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xubin
 */
public class DirectDamageSearchAdapter extends BaseMultiItemQuickAdapter<RecordRemandBean, BaseViewHolder> {

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable listDirectDamageSearchAdapter
     */
    public DirectDamageSearchAdapter(List<RecordRemandBean> data) {
        super(data);
        addItemType(RecordRemandBean.VIEW_TYPE_PARENT, R.layout.item_record_parent);
        addItemType(RecordRemandBean.VIEW_TYPE_CHILDREN, R.layout.item_record_children);
    }

    public void replaceList(List<RecordRemandBean> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        List<RecordRemandBean> newList = new ArrayList<>();
        for (RecordRemandBean bean : list) {
            bean.setItemViewType(RecordRemandBean.VIEW_TYPE_PARENT);
            newList.add(bean);
            List<SortBean> sortList = bean.getSortList();
            if (sortList == null || sortList.isEmpty()) {
                continue;
            }
            for (SortBean sortBean : sortList) {
                newList.add(createNewBean(bean, sortBean));
            }
        }
        setList(newList);
    }

    private RecordRemandBean createNewBean(RecordRemandBean bean, SortBean sortBean) {
        RecordRemandBean remandBean = new RecordRemandBean();
        sortBean.setParentRecordBean(bean);
        remandBean.setItemViewType(RecordRemandBean.VIEW_TYPE_CHILDREN);
        remandBean.setSortBean(sortBean);
        return remandBean;
    }

    public void add(SortBean sortBean) {
        if (sortBean == null) {
            return;
        }
        int position = 0;
        boolean add = false;
        RecordRemandBean recordRemandBean = null;
        for (int i = 0; i < getData().size(); i++) {
            RecordRemandBean bean = getData().get(i);
            if (RecordRemandBean.VIEW_TYPE_CHILDREN == bean.getItemType() && bean.getSortBean() != null) {
                SortBean sort = bean.getSortBean();
                if (sort.getRecordId() == null || sort.getApplyType() == null) {
                    continue;
                }
                if (sort.getSortId() == null) {
                    continue;
                }
                if (sort.getSortId().equals(sortBean.getSortId())
                        && sort.getApplyType().equals(sortBean.getApplyType())
                        && sort.getRecordId().equals(sortBean.getRecordId())) {
                    bean.setSortBean(sortBean);
                    setData(i, bean);
                    return;
                } else if (sort.getRecordId().equals(sortBean.getRecordId())) {
                    position = i;
                    add = true;
                    recordRemandBean = bean;
                }
            }
        }
        if (add) {
            addData(position, createNewBean(recordRemandBean, sortBean));
            return;
        }
        RecordRemandBean bean = sortBean.getParentRecordBean();
        if (bean == null) {
            return;
        }
        bean.setItemViewType(RecordRemandBean.VIEW_TYPE_PARENT);
        List<RecordRemandBean> newList = new ArrayList<>();
        newList.add(bean);
        newList.add(createNewBean(bean, sortBean));
        addData(newList);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, RecordRemandBean item) {
        int itemType = item.getItemType();
        if (RecordRemandBean.VIEW_TYPE_PARENT == itemType) {
            helper.setText(R.id.text_record, item.getRecordType())
                    .setText(R.id.tv_record, item.getRecordCode())
                    .setText(R.id.tv_user_name, item.getRemandUser());
        } else if ((RecordRemandBean.VIEW_TYPE_CHILDREN == itemType)) {
            SortBean sortBean = item.getSortBean();
            if (sortBean == null) {
                return;
            }
            helper.setText(R.id.tv_title, sortBean.getSortName())
                    .setText(R.id.tv_model, "型号：" + sortBean.getSortModel())
                    .setText(R.id.tv_returned_num, "待还：" + sortBean.needReturnNum())
                    .setText(R.id.tv_damage_num, "损坏：" + sortBean.excNum());
            helper.getView(R.id.cl_root)
                    .setOnClickListener(v -> nextDamageActivity(sortBean));
        }
    }

    private void nextDamageActivity(SortBean bean) {
        EventBus.getDefault().postSticky(new DamageRegisSearchEvent(bean));
        getContext().startActivity(new Intent(getContext(), DamageRegisProductActivity.class));
    }
}
